<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/column1';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	
	protected $key_token = "@trungan!@#";
	
	public function redirect_to($url, $params = null, $terminate = true, $statusCode = 302){
		if(is_array($url))
		{
			$route=isset($url[0]) ? $url[0] : '';
			$url=$this->createUrl($route, $this->getToken(array_splice($url,1)));
		}else{
			if ($params && !empty($params))
				$url=$this->createUrl($url, $this->getToken($params));
		}
		$this->redirect($url,$terminate,$statusCode);
	}
	
	public function getToken($params){
		if (!isset($params) || empty($params)) return null;
		$str = "";
		foreach ($params as $item){
			$str .= $item;
		}
		$str = md5($str.$this->key_token);
		return array_merge(array('token' => $str), $params);
	}
	
	public function checkToken(){
		$is_valid = false;
		
		if (isset($_GET['token']) && !empty($_GET['token'])){
			$params = $_GET;
			unset($params['token']);
			$str = "";
			foreach ($params as $item){
				$str .= $item;
			}
			
			$str = md5($str.$this->key_token);
			if ($_GET['token'] == $str) $is_valid = true;
		}
		
		if (!$is_valid){
			throw new CHttpException(404,'Dữ liệu không hợp lệ');
		}
	}
}